package com.henry.album.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;

import org.springframework.util.ObjectUtils;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

import net.coobird.thumbnailator.Thumbnails;

public class ImgUtils {

	public static final List<String> ImageSuffixArray = Arrays.asList("png", "jpeg", "wbmp", "bmp", "gif", "webp", "heif",
			"jpg", "ico", "svg");

	public static void main(String[] args) {

		// imageThumbnails(200, 100, "D:/yt.jpg", "D:/aa.jpg");

		System.out.println(getSuffix("sdjflksjf.jpg"));
	}

	public static boolean isImage(String filename) {

		String suffix = getSuffix(filename).toLowerCase();

		if (ImageSuffixArray.contains(suffix)) {

			return true;
		}

		return false;
	}

	public static String getSuffix(String filename) {

		if (ObjectUtils.isEmpty(filename)) {
			return "";
		}

		if (!filename.contains(".")) {

			return "";
		}

		return filename.substring(filename.lastIndexOf(".") + 1, filename.length());

	}

	/**
	 * 压缩输出缩率图
	 * 
	 * @param height
	 * @param width
	 * @param filePath
	 * @param outpath
	 * @return
	 */
	public static boolean imageThumbnails(Integer height, Integer width, String filePath, String outpath) {

		try {

			String fileSuffix = filePath.substring(filePath.lastIndexOf(".") + 1);

			File file = new File(filePath);

			FileInputStream is = new FileInputStream(file);

			BufferedImage bufferedImageBig = Thumbnails.of(is).forceSize(height, width).asBufferedImage(); // 大图字节转换

			String newPath = outpath;

			File newFile = new File(newPath);

			if (newFile.exists()) {
				newFile.delete();
			}

			newFile.createNewFile();

			ImageIO.write(bufferedImageBig, fileSuffix, newFile);
		} catch (Exception e) {

			e.printStackTrace();

			throw new RuntimeException("压缩图片有误");

		}

		return true;
	}

	/**
	 *  返回图片拍摄时间
	 * 
	 */
	public static String getImageTime(File jpegFile) {
		Metadata metadata;

		String phoneTime = "";

		try {
			metadata = JpegMetadataReader.readMetadata(jpegFile);
			Iterator<Directory> it = metadata.getDirectories().iterator();

			endLoop: // 跳出多层循环
			while (it.hasNext()) {
				Directory exif = it.next();
				Iterator<Tag> tags = exif.getTags().iterator();
				while (tags.hasNext()) {
					Tag tag = (Tag) tags.next();

					String tagName = tag.getTagName();

					if (!ObjectUtils.isEmpty(tagName)) {
						// Date/Time Original
						// Date/Time Digitized
						if (tagName.indexOf("Date/Time") != -1) {
							// System.out.println(tag.getTagName()+ ":"+tag.getDescription());
							phoneTime = tag.getDescription();
							break endLoop;
						}
					}

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return phoneTime;
	}

}